package top.chengdongqing.mishop.user

import org.springframework.http.HttpStatus
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*
import top.chengdongqing.mishop.user.dto.UserProfileDTO
import top.chengdongqing.mishop.user.vo.UserStatisticsVO

@RestController
@RequestMapping("/user")
class UserController(private val userService: UserService) {

    @GetMapping("/statistics")
    fun statistics(@AuthenticationPrincipal userId: Int): UserStatisticsVO {
        return userService.statistics(userId)
    }

    @PutMapping("/profile")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun updateProfile(@AuthenticationPrincipal userId: Int, @RequestBody profileDTO: UserProfileDTO) {
        userService.updateProfile(userId, profileDTO)
    }

    @PutMapping("/phone-number")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun modifyPhoneNumber(
        @AuthenticationPrincipal userId: Int,
        @RequestParam phoneNumber: String,
        @RequestParam verificationCode: String
    ) {
        userService.modifyPhoneNumber(userId, phoneNumber, verificationCode)
    }

    @PutMapping("/email-address")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun modifyEmailAddress(
        @AuthenticationPrincipal userId: Int,
        @RequestParam emailAddress: String,
        @RequestParam verificationCode: String
    ) {
        userService.modifyEmailAddress(userId, emailAddress, verificationCode)
    }

    @PutMapping("/password")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun modifyPassword(
        @AuthenticationPrincipal userId: Int,
        @RequestParam password: String
    ) {
        userService.modifyPassword(userId, password)
    }

    @DeleteMapping
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun deleteAccount(@AuthenticationPrincipal userId: Int) {
        userService.deleteAccount(userId)
    }
}